<?php

namespace app\xwam\controller;

/**
 * Description of LoginController
 *
 * @version 1.0 2015-10-20 SoChishun(14507247@qq.com) Added.
 */
class Login extends \think\Controller {

    protected $loginModel;
    protected $loginFilterModel;
    protected $confModel;
    protected $show_captcha;
    protected $show_sms_verify_code;
    protected $show_email_verify_code;

    protected function initialize() {
        parent::initialize();
        $this->loginModel = new \app\xwam\model\LoginModel();
        $this->loginFilterModel = new \app\xwam\model\LoginFilterModel();
        $this->confModel = new \app\xwam\model\SystemConfModel();
        // 初始登录不显示图形验证码，登录失败则显示图形验证码
        $this->show_captcha = $this->loginFilterModel->getLoginErrorCount() > 0;
        // 后台是否开启手机验证码功能
        $this->show_sms_verify_code = intval($this->confModel->getConfValue('show_sms_verify_code')) > 0;
        // 后台是否开启邮件验证码功能
        $this->show_email_verify_code = intval($this->confModel->getConfValue('show_email_verify_code')) > 0;
    }

    public function index() {
        $assign = [
            'show_captcha' => $this->show_captcha,
            'show_sms_verify_code' => $this->show_sms_verify_code,
            'show_email_verify_code' => $this->show_email_verify_code,
        ];
        return view('', ['showCaptcha' => $showCaptcha]);
    }

    public function doLogin($login_name = '', $password = '') {

        if (!$this->loginFilterModel->checkLoginable()) {
            exit('您已经出错5次, 请5分钟后再登录!');
        }

        $captcha = $this->request->post('captcha');
        $sms_code = $this->request->post('sms_code');
        $email_code = $this->request->post('email_code');
        if ($this->show_captcha) {
            if (!$captcha || $captcha != session('admin_login_captcha')) {
                $this->error('验证码错误!', url('index'), '', 1);
            }
        }
        if ($this->show_email_verify_code) {
            if (!$email_code || EmailVerifyCodeModel::verify($email_code)) {
                $this->error('邮件验证码错误!', url('index'), '', 1);
            }
        }
        if ($this->show_sms_verify_code) {
            if (!$sms_code || SmsVerifyCodeModel::verify($sms_code)) {
                $this->error('短信验证码错误!', url('index'), '', 1);
            }
        }
        if (!$login_name || !$password || (!is_null($captcha) && !$captcha)) {
            $this->loginFilterModel->setLoginErrorInfo();
            $this->error('表单填写不完整!', url('index'), '', 1);
        }
        $result = $this->loginModel->login($login_name, $password, $captcha);
        if ($result['status']) {
            // 保存登录信息到会话中
            $this->loginFilterModel->resetLoginErrorInfo();
            session('conf', $this->confModel->getConfData());
            $this->redirect('index/index');
        } else {
            $this->error(xsfn_message_format($result['info']), url('index'), '', 2);
        }
    }

    public function logout() {
        $this->loginModel->logout();
        header('location:' . url('index'));
        exit;
    }

}
